//
//  HOMeterListViewController.m
//  微度报修
//
//  Created by foguo-mac-1 on 7/1/16.
//  Copyright © 2016 WeiDu. All rights reserved.
//

#import "HOMeterListViewController.h"
#import "HOConstants.h"
#import "HOUtils.h"
#import "HOMeterRecordEntity.h"

@interface HOMeterListViewController ()

@end

@interface HOMeterListViewController ()

@end

@implementation HOMeterListViewController

- (void)viewDidLoad {
	[super viewDidLoad];
	// Do any additional setup after loading the view.
	[self initTableViewWithPullUp];
	[self requestDataWithMaxId:1];
}

- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	[self setNavigationItemTitle:NSLocalizedString(@"抄表列表", @"抄表列表")];
	
}

- (void)requestDataWithMaxId:(int)maxId
{
	[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_MetersList,
								  kHO_FIELD_MaxID:[NSNumber numberWithInt:maxId]}];
}

- (void)pullUpToRefresh
{
	HOMeterRecordEntity *entity = [self.tableViewDataList lastObject];
	if(entity)
	{
		[self requestDataWithMaxId:entity.meterId];
	}
	else
	{
		[self requestDataWithMaxId:1];
	}
}


#pragma mark - -- UITableViewDelegate/UITableViewDataResource implement methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	HOMeterRecordEntity *entity = [self.tableViewDataList objectAtIndex:indexPath.section];
	UILabel *locationLbl = [[[UILabel alloc]
				   initWithFrame:CGRectMake(HO_HORIZONAL_PADDING,
											0,
											tableView.frame.size.width - 2 * HO_HORIZONAL_PADDING,
											60)] autorelease];
	locationLbl.numberOfLines = 3;
	locationLbl.text = [NSString stringWithFormat:@"%@：%@", NSLocalizedString(@"详细地址", @"详细地址"), entity.meterAddress];
	[locationLbl sizeToFit];
	return 60 + locationLbl.frame.size.height;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	NSInteger count = [self.tableViewDataList count];
	[self updateNoResultView:count];;
	return count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return 1;
}


- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
	if(section == 0)
		return [HOUtils defaultOnePixelConversion];
	return SECTION_PADDING / 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *HOTableViewCell = @"HOTableViewCell";
	UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:HOTableViewCell];
	if(cell == nil)
	{
		cell = [[[UITableViewCell alloc]
				 initWithStyle:UITableViewCellStyleDefault
				 reuseIdentifier: HOTableViewCell] autorelease];
	}
	HOMeterRecordEntity *entity = [self.tableViewDataList objectAtIndex:indexPath.section];
	UILabel *titleLbl = (UILabel *)[cell viewWithTag:115];
	if(titleLbl == nil)
	{
		titleLbl = [[UILabel alloc]
					initWithFrame:CGRectMake(HO_HORIZONAL_PADDING - 3,
											 8,
											 tableView.frame.size.width - 225,
											 22)];
		titleLbl.tag = 115;
		[cell.contentView addSubview:titleLbl];
		titleLbl.textColor = TEXT_COLOR_666666;
		titleLbl.font = [UIFont systemFontOfSize:15];
		[titleLbl release];
	}
	titleLbl = (UILabel *)[cell viewWithTag:115];
	titleLbl.text = entity.meterType;
	
	UILabel *timeLbl = (UILabel *)[cell viewWithTag:116];
	if(timeLbl == nil)
	{
		timeLbl = [[UILabel alloc]
				   initWithFrame:CGRectMake(baseTableview.frame.size.width - 160,
											titleLbl.frame.origin.y,
											150,
											22)];
		timeLbl.tag = 116;
		timeLbl.textAlignment = NSTextAlignmentRight;
		[cell.contentView addSubview:timeLbl];
		timeLbl.textColor = TEXT_COLOR_999999;
		timeLbl.font = [UIFont systemFontOfSize:13];
		[timeLbl release];
	}
	timeLbl = (UILabel *)[cell viewWithTag:116];
	timeLbl.text = [HOUtils getDateFromTimeInterval:entity.time * 1000];
	
	UILabel *contentLbl = (UILabel *)[cell viewWithTag:117];
	if(contentLbl == nil)
	{
		contentLbl = [[UILabel alloc]
					  initWithFrame:CGRectMake(titleLbl.frame.origin.x,
											   22 + 3 + 8,
											   tableView.frame.size.width - 2 * HO_HORIZONAL_PADDING,
											   22)];
		contentLbl.tag = 117;
		contentLbl.numberOfLines = 3;
		[cell.contentView addSubview:contentLbl];
		contentLbl.textColor = TEXT_COLOR_666666;
		contentLbl.font = [UIFont systemFontOfSize:13];
		[contentLbl release];
	}
	contentLbl = (UILabel *)[cell viewWithTag:117];
	contentLbl.text = [NSString stringWithFormat:@"%@:%0.1f", NSLocalizedString(@"数值", @"数值"), entity.meterValue];
	
	UILabel *locationLbl = (UILabel *)[cell viewWithTag:118];
	if(locationLbl == nil)
	{
		locationLbl = [[UILabel alloc]
					   initWithFrame:CGRectMake(titleLbl.frame.origin.x,
												contentLbl.frame.origin.y + contentLbl.frame.size.height + 4,
											   tableView.frame.size.width - 2 * HO_HORIZONAL_PADDING,
											   60)];
		locationLbl.tag = 118;
		locationLbl.numberOfLines = 3;
		[cell.contentView addSubview:locationLbl];
		locationLbl.textColor = TEXT_COLOR_999999;
		locationLbl.font = [UIFont systemFontOfSize:12];
		[locationLbl release];
	}
	locationLbl = (UILabel *)[cell viewWithTag:118];
	locationLbl.text = [NSString stringWithFormat:@"%@：%@", NSLocalizedString(@"详细地址", @"详细地址"), entity.meterAddress];
	[locationLbl sizeToFit];
	
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	
}
#pragma mark

- (HOEditTableViewCell *)tableView:(UITableView *)tableView cellForEditRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *AMBaseTableViewCellIdentifier = @"HOTableViewCell";
	HOEditTableViewCell *cell = (HOEditTableViewCell *)[tableView dequeueReusableCellWithIdentifier:AMBaseTableViewCellIdentifier];
	if(cell == nil)
	{
		cell = [[[HOEditTableViewCell alloc]
				 initWithStyle:UITableViewCellStyleDefault
				 reuseIdentifier: AMBaseTableViewCellIdentifier] autorelease];
	}
	return cell;
}
#pragma mark -- tool
- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
	// Dispose of any resources that can be recreated.
}


- (void)requestDidSuccessWithResponse:(HOBaseResponseEntity *)response
{
	if([response isEqualAction:kHO_ACTION_MetersList])
	{
		for(int i = 0; i < [response.dataNode count]; i ++)
		{
			NSDictionary *dic = [response.dataNode objectAtIndex:i];
			HOMeterRecordEntity *entity = [[HOMeterRecordEntity alloc] init];
			[entity parseDic:dic];
			[self.tableViewDataList addObject:entity];
			[entity release];
		}
		[self doneLoadingTableViewRefresh];
	}
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */


@end
